package com.lg.meng.base;

import com.lg.meng.model.BaseServerResponse;

import java.util.List;

import io.reactivex.Observable;

public abstract class BaseSingleListPresenter<P extends BaseServerResponse, T, V extends BaseSingleListView<P, T>> extends BasePresenter<V> {
    public abstract void loadData();

    protected void loadData(Observable<P> observable) {
        if (getView() == null) {
            return;
        }
        observable.subscribe(simpleObserver(new Callback<P>() {
            @Override
            public void resultSuccess(P p) {
                List<T> datas = getList(p);
                if (datas != null) {
                    view.loadSingleListSuccess(datas);
                } else {
                    view.loadSingleListFailed();
                }
            }

            @Override
            public void resultError(Throwable e) {
                view.loadSingleListFailed();
            }
        }));
    }

    protected abstract List<T> getList(P datas);
}
